package com.localization;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.myprojectoffeixue.AESUtil;
import com.example.myprojectoffeixue.GateWay;
import com.example.myprojectoffeixue.MyApplication;
import com.example.myprojectoffeixue.R;
import com.example.myprojectoffeixue.ToastUtils;
import com.example.myprojectoffeixue.UserClass;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class FristActivity extends Activity {
    MyApplication m;

   // byte cmd[] = {(byte) 0xfc,0x00,0x0b,0x00,0x01,0x00,0x08,(byte)0xb8,0x76,0x0b,0x14,0x01,(byte)0xff,0x01,0x2c};

    SocketUdpClient udp;
    String cmd2;
    String cmd3;
    boolean flag=false;
    Handler hand=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0){
            Toast.makeText(FristActivity.this,"命令发送成功",Toast.LENGTH_SHORT).show();}else{
                Toast.makeText(FristActivity.this,"请确认连接public204",Toast.LENGTH_SHORT).show();
            }
            super.handleMessage(msg);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frist_login);
        GetDeviceList();

    }
    @Override
    protected void onStart() {
        super.onStart();
    }





    @Override
    protected void onDestroy() {
        flag=false;

        super.onDestroy();
    }




    public void GetDeviceList(){

        m= (MyApplication) getApplication();
        if (m.username != null) {
            GateWay Gw = new GateWay();
            Gw.userId = m.userId;
            Gw.status = 1;
            String j = JSON.toJSONString(Gw);
            Log.d("hh",j);
            j = AESUtil.encrypt(j);
            HttpUtils httpUtils = new HttpUtils();
            RequestParams params = new RequestParams();
            params.setHeader("Content-Type", "application/json");
            try {
                params.setBodyEntity(new StringEntity(j, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            httpUtils.send(com.lidroid.xutils.http.client.HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/device/getDeviceList", params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(com.lidroid.xutils.exception.HttpException arg0, String arg1) {

                            ToastUtils.showToast(getBaseContext(), "网络异常");
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> arg0) {
                            if (arg0.result != null && !("".equals(arg0.result))) {
                                arg0.result = AESUtil.decrypt(arg0.result);
                                Log.d("arg0", arg0.result);
                                com.alibaba.fastjson.JSONObject jb = com.alibaba.fastjson.JSONObject.parseObject(arg0.result);
                                String status = jb.getString("status");
                                if ("0".equals(status)) {
                                    JSONArray ja = jb.getJSONArray("deviceList");
                                    SocketCmdsDistribute.m_gateway = (ArrayList<GateWay>) JSON.parseArray(ja.toString(), GateWay.class);
                                    Log.d("hh","xxxxxxx");
                                }
                            }
                        }

                    });

        }
    }











}



